Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.requestSignature = requestSignature, exports.requestAccesstoken = requestAccesstoken, 
exports.requestOpenId = requestOpenId, exports.requestAccesstokenIfExist = requestAccesstokenIfExist, 
exports.requestEncodeFields = requestEncodeFields, exports.requestUseUploadIfNeccesary = requestUseUploadIfNeccesary, 
exports.responseResolveOnlySuccess = responseResolveOnlySuccess;

var _apiSignature = require("./api-signature"), _apiCode = _interopRequireDefault(require("./api-code")), _util = _interopRequireDefault(require("./util")), _user = _interopRequireDefault(require("./user"));

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function _objectSpread(r) {
    for (var e = 1; e < arguments.length; e++) {
        var t = null != arguments[e] ? arguments[e] : {}, n = Object.keys(t);
        "function" == typeof Object.getOwnPropertySymbols && (n = n.concat(Object.getOwnPropertySymbols(t).filter(function(e) {
            return Object.getOwnPropertyDescriptor(t, e).enumerable;
        }))), n.forEach(function(e) {
            _defineProperty(r, e, t[e]);
        });
    }
    return r;
}

function _defineProperty(e, r, t) {
    return r in e ? Object.defineProperty(e, r, {
        value: t,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : e[r] = t, e;
}

function joinUrl(e, r) {
    return "string" == typeof e ? e + (e.includes("?") ? "&" : "?") + r : "";
}

function requestSignature(e) {
    var r = e.url, t = e.method, n = e.data, o = e.params, s = "get" === t ? n : o, u = _util.default.parseUrl(r).query;
    return e.url = joinUrl(r, (0, _apiSignature.generateSignParamsStr)(_objectSpread({}, u, s))), 
    Promise.resolve(e);
}

function requestAccesstoken(r) {
    return _user.default.getTokens().then(function(e) {
        return e && e.accesstoken ? _objectSpread({}, r, {
            url: joinUrl(r.url, "accesstoken=".concat(e.accesstoken))
        }) : Promise.reject();
    });
}

function requestOpenId(r) {
    return _user.default.getTokens().then(function(e) {
        return _objectSpread({}, r, {
            data: Object.assign({}, r.data, {
                openid: e.openid
            })
        });
    });
}

function requestAccesstokenIfExist(r) {
    return requestAccesstoken(r).catch(function(e) {
        return r;
    });
}

function requestEncodeFields(r) {
    if (r.formData) {
        var e = Object.keys(r.formData);
        e && e.length && (r.formData = _objectSpread({}, r.formData), e.forEach(function(e) {
            r.formData[e] = encodeURIComponent(r.formData[e]);
        }), r.formData.encode = 1);
    }
    return Promise.resolve(r);
}

function requestUseUploadIfNeccesary(e) {
    return e.name && e.filePath && (e.method = "UPLOAD"), Promise.resolve(e);
}

function responseResolveOnlySuccess(e) {
    return e.code === _apiCode.default.SUCCESS ? Promise.resolve(e) : (_util.default.qAlert(e.message || "网络错误，请重新尝试!", "操作失败"), 
    Promise.reject(e));
}